package com.bao123.redis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * redis string操作工具类
 * @author lbw
 */
@Component
public class RedisStringService {

    @Autowired
    private StringRedisTemplate template;

    /**
     * 设置不带过期时间的缓存
     * @param key
     * @param value
     */
    public void setKey(String key, String value){
        template.opsForValue().set(key, value);
    }
    /**
     * 设置带过期时间的缓存
     * @param key   key
     * @param value value
     * @param time  过期时间，单位为秒
     */
    public void setKey(String key, String value, long time){
        template.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    /**
     * 根据key获取value
     * @param key
     * @return
     */
    public String getValue(String key){
        return key == null ? null :template.opsForValue().get(key);
    }

    public void deleteKey(String key){
        template.delete(key);
    }

    public void deleteKeys(List<String> keys){
        template.delete(keys);
    }

    public Boolean hasKey(String key){
        return template.hasKey(key);
    }
}

